package com.example.demo2.student;

import com.example.demo2.Util.DBHelper;
import com.example.demo2.pojo.class1;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(name = "selectstudentSevlert", value = "/selectstudentSevlert")
public class selectstudentSevlert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PreparedStatement preparedStatement = null;
        Connection conn = null;
        ResultSet rs = null;
        DBHelper dBHelper = new DBHelper();
        Connection conn1 = dBHelper.getConn();
        ArrayList<class1> class1s = new ArrayList<>();
        try {
            preparedStatement = conn1.prepareStatement("select *from webdesign.class");
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                class1 class1 = new class1();
                class1.setId(rs.getInt("id"));
                class1.setClassName(rs.getString("className"));
                class1s.add(class1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            dBHelper.closeAll(conn, preparedStatement, rs);
        }
        req.setAttribute("list", class1s);
        RequestDispatcher dispatcher = req.getRequestDispatcher("classslist.jsp");
        dispatcher.forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PreparedStatement preparedStatement = null;
        Connection conn = null;
        ResultSet rs = null;
        DBHelper dBHelper = new DBHelper();
        Connection conn1 = dBHelper.getConn();
        ArrayList<class1> class1s = new ArrayList<>();
        try {
            preparedStatement = conn1.prepareStatement("select *from webdesign.class");
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                class1 class1 = new class1();
                class1.setId(rs.getInt("id"));
                class1.setClassName(rs.getString("className"));
                class1s.add(class1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            dBHelper.closeAll(conn, preparedStatement, rs);
        }
        req.setAttribute("list", class1s);
        RequestDispatcher dispatcher = req.getRequestDispatcher("addStudent.jsp");
        dispatcher.forward(req, resp);


    }
}
